package com.hghivln.erpwarehouse.business.controller;


import com.hghivln.erpwarehouse.business.entity.Customer;
import com.hghivln.erpwarehouse.business.service.CustomerService;
import com.hghivln.erpwarehouse.business.vo.CustomerVo;
import com.hghivln.erpwarehouse.system.common.lang.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hghivln
 * @since 2021-07-10
 */
@RestController
@RequestMapping("api/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @RequestMapping("loadAllCustomer")
    public Object loadAllCustomer(CustomerVo customerVo) {
        return customerService.QueryAllCustomer(customerVo);
    }

    @RequiresPermissions("customer:add")
    @RequestMapping("addCustomer")
    public Result addCustomer(Customer customer) {
        try {
            customerService.addCustomer(customer);
            return Result.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ADD_ERROR;
        }
    }

    @RequiresPermissions("customer:update")
    @RequestMapping("updateCustomer")
    public Result updateCustomer(Customer customer) {
        try {
            customerService.updateCustomer(customer);
            return Result.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.UPDATE_ERROR;
        }
    }

    @RequiresPermissions("customer:delete")
    @RequestMapping("deleteCustomer")
    public Result deleteCustomer(Integer id) {
        try {
            customerService.removeById(id);
            return Result.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.DELETE_ERROR;
        }
    }

    @RequiresPermissions("customer:bachdelete")
    @RequestMapping("batchDeleteCustomer")
    public Result batchDeleteCustomer(Integer[] ids){
        try {
            Collection<Serializable> idList = new ArrayList<>(Arrays.asList(ids));
            customerService.removeByIds(idList);
            return Result.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.DELETE_ERROR;
        }
    }

    @RequestMapping("getAllAvailableCustomer")
    public Object getAllAvailableProvider(){
        return customerService.getAllAvailableCustomer();
    }

}
